package com.taptech.doufu.ui.view.weex;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;

import com.taptech.doufu.ui.activity.NewPersonalNoteDesActivity;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.event.EventBusCollectUnRead;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.personalcenter.PersonalInviteWindow;
import com.taptech.doufu.util.ShareUtils;
import com.taptech.doufu.util.UIUtil;

/**
 * Created by Wyatt on 2018/1/26/026.
 */

// js通信接口
public class TFJavascriptInterface {
    private final int THIRD_PARTY_SHARE = 7;
    private Context context;
    private WebView mWebView;

    public TFJavascriptInterface(Context context,WebView webView) {
        this.context = context;
        this.mWebView = webView;
    }

    @android.webkit.JavascriptInterface
    public void login() {
        //清除
        EventBusCollectUnRead.clearUnRead();
        AccountService.getInstance().clearAccount();
        AccountService.getInstance().setAccountBack(true);
        AccountService.getInstance().jumpToLogin();
    }


    @android.webkit.JavascriptInterface
    public void thirdPartyShare(String data) {
        Message msg = new Message();
        msg.what = THIRD_PARTY_SHARE;
        msg.obj = data;
        mHandler.sendMessage(msg);
    }



    @android.webkit.JavascriptInterface
    public void invite() {
        new PersonalInviteWindow((Activity) context, 0, null);
    }


    @android.webkit.JavascriptInterface
    public void openWeexPage(String url) {
        SimpleWeexActivity.startActivity(context,url);
    }


    @android.webkit.JavascriptInterface
    public void getUserIdFromNative(){
        mWebView.loadUrl("javascript:getUserId('"+AccountService.getInstance().getUserUid()+"')");
    }




    @android.webkit.JavascriptInterface
    public void toast(String content) {
        UIUtil.toastMessage(context, content);
    }

    @android.webkit.JavascriptInterface
    public void jumpToArticleDes(String id, int object_type) {
        Intent intent = null;
        switch (object_type) {
            case Constant.OBJECT_TYPE_NOVEL:
                NovelDetailsActivity.Companion.startActivity(context,id);
                break;
            case Constant.OBJECT_TYPE_DRAW:
                intent = new Intent(context, NewPersonalNoteDesActivity.class);
                intent.putExtra(Constant.ARTICLE_ID, id);
                intent.putExtra(Constant.OBJECT_TYPE, String.valueOf(object_type));
                context.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_CARTOON:
                SimpleWeexActivity.startActivity(context,"TFNovel/TFComicDetail.js?id="+id,null);
                break;
        }
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case THIRD_PARTY_SHARE:
                    if (msg.obj != null) {
                        String data = msg.obj.toString();
                        ShareUtils.thirdPartyShare(context,data,true);
                    }
                    break;
                default:
                    break;
            }

        }
    };
}
